﻿using System;
using System.Runtime.InteropServices;
using System.Xml;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Resources;
using Microsoft.SharePoint;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;

namespace Version3.SharePoint
{
    //This is the control that renders on SharePoint column configuration
    //the host control is ReadUnreadMarksDataFieldPropertyEditor.ascx
    //page. Nothing fancy here, just plain old C# codebehind
    [Guid("A21EF500-6F3B-493e-BF2C-39FB51831997")]
    [CLSCompliant(false)]
    public class ReadUnreadMarksDataFieldPropertyEditor : UserControl, IFieldEditor
    {
        #region Declarations
        protected RadioButton radColumnDisplayIconic;
        protected RadioButton radColumnDisplayBold;
        protected RadioButton radColumnRetainReadMarks;
        protected RadioButton radColumnClearReadMarks;
        protected CheckBox chkShowInlineEditingTools;
        protected CheckBox chkWriteDirect;
        protected CheckBox chkInvisibleOnForm;
        protected TextBox txtReadImageUrl;
        protected TextBox txtUnreadImageUrl;
        protected TextBox txtUnreadHTMLColor;
        protected Label lblReadImageUrl;
        protected Label lblUnreadImageUrl;

        private string _ReadImageUrl = string.Empty;
        private string _UnreadImageUrl = string.Empty;
        private string _UnreadHTMLColor = string.Empty;

        private bool _ClearReadMarksOnUpdate = false;
        private bool _ShowInlineEditingTools = false;
        private bool _WriteDirect= false;
        private bool _VisibleOnForm = false;

        private string _ColumnRenderMode = string.Empty;

        #endregion

        #region UserControl imlementation
        public override void Focus()
        {
            EnsureChildControls();
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            if (!Page.IsPostBack)
            {
                txtUnreadImageUrl.Enabled = false;
                txtReadImageUrl.Enabled = false;
                radColumnDisplayIconic.Checked = false;
                radColumnDisplayBold.Checked = false;
                radColumnClearReadMarks.Checked = _ClearReadMarksOnUpdate;
                radColumnRetainReadMarks.Checked = !_ClearReadMarksOnUpdate;
                chkShowInlineEditingTools.Checked = _ShowInlineEditingTools;
                chkWriteDirect.Checked = _WriteDirect;
                chkInvisibleOnForm.Checked = !_VisibleOnForm;
                txtUnreadImageUrl.Text = _UnreadImageUrl;
                txtReadImageUrl.Text = _ReadImageUrl;
                txtUnreadHTMLColor.Text = _UnreadHTMLColor;
                switch (_ColumnRenderMode)
                {
                    case ReadUnreadMarksDataField.ICON_DISPLAY:
                        radColumnDisplayIconic.Checked = true;
                        break;
                    case ReadUnreadMarksDataField.BOLD_DISPLAY:
                    default:
                        radColumnDisplayBold.Checked = true;
                        break;
                }

            }
            RadioButton_CheckedChanged(null, null);
            radColumnDisplayBold.CheckedChanged += new EventHandler(RadioButton_CheckedChanged);
            radColumnDisplayIconic.CheckedChanged += new EventHandler(RadioButton_CheckedChanged);
        }

        #endregion

        #region Utilities
        void RadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (radColumnDisplayIconic.Checked)
            {
                txtUnreadImageUrl.Enabled = true;
                txtReadImageUrl.Enabled = true;
                txtUnreadImageUrl.Visible = true;
                txtReadImageUrl.Visible = true;
                lblUnreadImageUrl.Visible = true;
                lblReadImageUrl.Visible = true;

                txtUnreadHTMLColor.Text = string.Empty;
                txtUnreadHTMLColor.Visible = false;
                txtUnreadHTMLColor.Enabled = false;
            }
            else
            {
                txtUnreadImageUrl.Text = string.Empty;
                txtReadImageUrl.Text = string.Empty;
                txtUnreadImageUrl.Enabled = false;
                txtReadImageUrl.Enabled = false;
                txtUnreadImageUrl.Visible = false;
                txtReadImageUrl.Visible = false;
                lblUnreadImageUrl.Visible = false;
                lblReadImageUrl.Visible = false;

                txtUnreadHTMLColor.Visible = true;
                txtUnreadHTMLColor.Enabled = true;
            }
            
        }

        private string ColumnRenderMode()
        {
            if (radColumnDisplayIconic.Checked) return ReadUnreadMarksDataField.ICON_DISPLAY;
            return ReadUnreadMarksDataField.BOLD_DISPLAY;
        }
        #endregion

        #region IFieldEditor implementation
        public bool DisplayAsNewSection
        {
            get
            {
                return true;
            }
        }

        public void InitializeWithField(SPField field)
        {
            if (null != field)
            {
                ReadUnreadMarksDataField sharePointField = (ReadUnreadMarksDataField)field;
                _ClearReadMarksOnUpdate = sharePointField.ClearReadMarksOnUpdate;
                _ColumnRenderMode = sharePointField.ColumnRenderMode;
                _ShowInlineEditingTools = sharePointField.ShowInlineEditingTools;
                _WriteDirect = sharePointField.WriteDirect;
                _VisibleOnForm = sharePointField.VisibleOnForm;
                _ReadImageUrl = sharePointField.ReadImageUrl;
                _UnreadImageUrl = sharePointField.UnreadImageUrl;
                _UnreadHTMLColor = sharePointField.UnreadHTMLColor;
            }
        }


        public void OnSaveChange(SPField field, bool isNewField)
        {
            ReadUnreadMarksDataField sharePointField = (ReadUnreadMarksDataField)field;
            sharePointField.ColumnRenderMode = ColumnRenderMode();
            sharePointField.ClearReadMarksOnUpdate = radColumnClearReadMarks.Checked;
            sharePointField.ShowInlineEditingTools = chkShowInlineEditingTools.Checked;
            sharePointField.WriteDirect = chkWriteDirect.Checked;
            sharePointField.VisibleOnForm = !chkInvisibleOnForm.Checked;
            sharePointField.UnreadImageUrl = txtUnreadImageUrl.Text;
            sharePointField.ReadImageUrl = txtReadImageUrl.Text;
            sharePointField.UnreadHTMLColor = txtUnreadHTMLColor.Text;
            if (isNewField)
            {
                //if the field is new, we will set the global new property cache
                //which helps us avoid all of the conflicts between update states
                //that can occur during postback processing of the control
                sharePointField.SetNewColumnPropertyCacheValue(ReadUnreadMarksDataField.COLUMN_RENDER_MODE_KEY, ColumnRenderMode());
                sharePointField.SetNewColumnPropertyCacheValue(ReadUnreadMarksDataField.CLEAR_READ_MARKS_ON_UPDATE_KEY, radColumnClearReadMarks.Checked.ToString());
                sharePointField.SetNewColumnPropertyCacheValue(ReadUnreadMarksDataField.SHOW_INLINE_EDITING_TOOLS_KEY, chkShowInlineEditingTools.Checked.ToString());
                sharePointField.SetNewColumnPropertyCacheValue(ReadUnreadMarksDataField.VISIBLE_ON_FORM_KEY, (!chkInvisibleOnForm.Checked).ToString());
                sharePointField.SetNewColumnPropertyCacheValue(ReadUnreadMarksDataField.WRITE_DIRECT_KEY, chkWriteDirect.Checked.ToString());
                sharePointField.SetNewColumnPropertyCacheValue(ReadUnreadMarksDataField.UNREAD_IMAGE_URL_KEY, txtUnreadImageUrl.Text);
                sharePointField.SetNewColumnPropertyCacheValue(ReadUnreadMarksDataField.READ_IMAGE_URL_KEY, txtReadImageUrl.Text);
                sharePointField.SetNewColumnPropertyCacheValue(ReadUnreadMarksDataField.UNREAD_HTML_COLOR_KEY, txtUnreadHTMLColor.Text);
            }
        }
        #endregion

    }
}
